﻿' Copyright (c) Microsoft Corporation. All rights reserved.
Option Strict Off
Imports System.Xml
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Text

Public Class BrowserWithoutTabs
#Region " API "
    'This is the API to get an icon of a favourite
    Private Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDislayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure

    Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr

    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_LARGEICON = &H0
    Private nIndex
#End Region

    <DllImport("user32.dll")> _
    Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    End Function


    Dim i As Integer = 0
    'Friend WithEvents tsbWebSite1 As System.Windows.Forms.ToolStripButton

    'Dim xmldoc As New XmlDocument

    'Private Sub GoToUrl(ByVal sWebsite As String)
    'Dim xPath As String = "//WebSite[Name='" & sWebsite & "']/URL"
    'Dim xn As XmlNode = xmldoc.SelectSingleNode(xPath)

    'CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).Url = New Uri(xn.InnerText)
    'End Sub

    Private Sub tsbBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbBack.Click
        AppManager.MainForm = Me
        If AppManager.CurrentBrowser.CanGoBack Then
            AppManager.CurrentBrowser.GoBack()
        End If
    End Sub

    Private Sub tsbForward_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbForward.Click
        AppManager.MainForm = Me
        If AppManager.CurrentBrowser.CanGoForward Then
            AppManager.CurrentBrowser.GoForward()
        End If
    End Sub

    Private Sub tsbHome_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbHome.Click
        AppManager.MainForm = Me
        If My.Settings.UseBBrowserHomePage = "True" Then
            AppManager.CurrentBrowser.Navigate(My.Settings.BBrowserHomePageUrl)
        ElseIf My.Settings.UseBBrowserHomePage = "False" Then
            AppManager.CurrentBrowser.GoHome()
        End If
    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbRefresh.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.Refresh()
    End Sub

    Private Sub BrowserWithoutTabs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        AppManager.MainForm = Me
    End Sub

    Private Sub BrowserWithoutTabs_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        
    End Sub

    'Private Sub tsbManage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbManage.Click
    'Dim frmManage As New Sites
    'frmManage.ShowDialog()

    'AddToolStripButtons()
    'End Sub

    Private Sub Browser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.F1) Then
            AppManager.MainForm = Me
            AboutBox1.ShowDialog()
        ElseIf (e.KeyCode = Keys.F5) Then
            AppManager.MainForm = Me
            AppManager.CurrentBrowser.Refresh()
        ElseIf (e.KeyCode = Keys.ControlKey) And (e.KeyCode = Keys.P) Then
            AppManager.MainForm = Me
            PrintDialog1.ShowDialog()
            AppManager.CurrentBrowser.Print()
        ElseIf (e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.O) Then
            AppManager.MainForm = Me
            Dim Open As New OpenFileDialog()
            Open.Title = "Open Local Webpage"
            Open.CheckFileExists = True
            Open.Filter = "All Files (*.*)|*.*"
            Open.ShowDialog()
            Try
                AppManager.CurrentBrowser.Navigate(Open.FileName)
            Catch ex As Exception
                'Do nothing on Exception
            End Try
        ElseIf (e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.X) Then
            Me.Close()
        ElseIf (e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.U And e.KeyCode = Keys.C) Then
            AppManager.MainForm = Me
            My.Computer.Clipboard.Clear()
            My.Computer.Clipboard.SetText(txtUrl1.Text)
        ElseIf (e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.W And e.KeyCode = Keys.S) Then
            AppManager.MainForm = Me
            AppManager.CurrentBrowser.ShowSaveAsDialog()
        End If
    End Sub

    Private Sub frmBrowser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'AddToolStripButtons()
        My.Settings.IsFormLoading = True
        My.Settings.IsFormLoadingExceptURL = True
        My.Settings.IsFirstTimeLoad = True
        My.Settings.Save()
        LoadFavourites()
        KeyPreview = True

        AppManager.MainForm = Me
        Dim ofrm As New frmBrowser
        Dim oTab As New BBrowser.TabPage(ofrm)
        Me.WebBrowser1.TabPages.Add(oTab.Form)

        WebBrowser1.TabPages(0).CloseButtonVisible = False

        'Dim svd As SHDocVw.WebBrowser = DirectCast(ofrm.WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser)
        'AddHandler svd.NewWindow3, AddressOf NewWindow3

        My.Settings.IsFormLoadingExceptURL = False
        My.Settings.Save()

        If My.Settings.NewWindowCalled = True Then
            ofrm.WebBrowser1.Navigate(My.Settings.NewWindowURL)
        ElseIf My.Settings.NewWindowCalled = False Then
            If My.Settings.ShowHomePageAtStartUp = "Yes" Then
                If My.Settings.UseBBrowserHomePage = "True" Then
                    ofrm.WebBrowser1.Navigate(My.Settings.BBrowserHomePageUrl)
                ElseIf My.Settings.UseBBrowserHomePage = "False" Then
                    ofrm.WebBrowser1.GoHome()
                End If
            ElseIf My.Settings.ShowHomePageAtStartUp = "No (Show Blank Page)" Then
                ofrm.WebBrowser1.Navigate("about:blank")
            ElseIf My.Settings.ShowHomePageAtStartUp = "No (Show Google)" Then
                ofrm.WebBrowser1.Navigate("http://www.google.co.uk")
            End If
        End If

        My.Settings.WindowsOpen += 1
        My.Settings.NewWindowCalled = False
        My.Settings.IsFormLoading = False
        'My.Settings.NewWindowCalled = False
        My.Settings.IsFormOpenTabs = True
        My.Settings.TimesLoadUntilNewsReminder -= 1
        My.Settings.Save()



        Timer3.Enabled = True
        Timer3.Start()
    End Sub

    'Private Sub AddTSButton(ByVal buttonName As String, ByVal webSiteName As String)
    'Dim newbutton As New System.Windows.Forms.ToolStripButton

    'newbutton.Name = buttonName
    'newbutton.Text = webSiteName
    'newbutton.DisplayStyle = ToolStripItemDisplayStyle.Text

    'ToolStrip3.Items.Add(newbutton)

    'AddHandler newbutton.Click, AddressOf tsbutton_Click

    'End Sub

    'Private Sub AddToolStripButtons()
    ' Load the XML file with user defined Web sites.
    'xmldoc.Load("C:\WebSites.xml")

    ' TEmpty the toolstrip with the user defined Web sites
    'ToolStrip3.Items.Clear()

    ' Loop through the XML file and add a toolstrip button for each
    ' user defined Web site.
    'Dim websiteNodes As XmlNodeList
    'Dim webSiteNode As XmlNode
    'websiteNodes = xmldoc.GetElementsByTagName("WebSite")
    'Dim iCount As Integer = 1

    'For Each webSiteNode In websiteNodes
    'Dim eachSiteNodes As XmlNodeList
    'Dim eachSiteNode As XmlNode
    'eachSiteNodes = webSiteNode.ChildNodes

    'For Each eachSiteNode In eachSiteNodes
    'If eachSiteNode.Name = "Name" Then
    'AddTSButton("tsWebsite" & iCount.ToString, eachSiteNode.InnerText)
    'iCount += 1
    'End If
    'Next
    'Next

    'End Sub

    'Private Sub tsbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'Dim aButton As ToolStripButton = CType(sender, ToolStripButton)
    'GoToUrl(aButton.Text)
    'End Sub

    Private Sub tsbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsbGo.Click
        AppManager.MainForm = Me
        If Not WebBrowser1.TabPages.Count = 0 Then
            AppManager.CurrentBrowser.Navigate(txtUrl.Text)
        ElseIf WebBrowser1.TabPages.Count = 0 Then
            Dim ofrm As New frmBrowser
            Dim oTab As New BBrowser.TabPage(ofrm)
            AppManager.AddTab(oTab)

            ofrm.WebBrowser1.Navigate(txtUrl.Text)
        End If
        Try
            AppManager.CurrentBrowser.Url = New Uri(txtUrl.Text)
        Catch
        End Try
    End Sub

    Private Sub txtUrl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUrl.Click
        AppManager.MainForm = Me
        If MouseButtons = Windows.Forms.MouseButtons.Left Then
            AppManager.CurrentBrowser.Stop()
        End If
    End Sub

    Private Sub tsbStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbStop.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.Stop()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        AppManager.MainForm = Me
        webStatus.Text = AppManager.CurrentBrowser.StatusText
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowPrintDialog()
    End Sub

    Private Sub txtUrl_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtUrl.KeyDown
        AppManager.MainForm = Me
        If (e.KeyCode = Keys.Enter) Then
            If Not WebBrowser1.TabPages.Count = 0 Then
                AppManager.CurrentBrowser.Navigate(txtUrl.Text)
            ElseIf WebBrowser1.TabPages.Count = 0 Then
                Dim ofrm As New frmBrowser
                Dim oTab As New BBrowser.TabPage(ofrm)
                AppManager.AddTab(oTab)

                ofrm.WebBrowser1.Navigate(txtUrl.Text)
            End If
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        AppManager.MainForm = Me
        Dim Open As New OpenFileDialog()
        Open.Title = "Open Local Webpage"
        Open.CheckFileExists = True
        Open.Filter = "All Files (*.*)|*.*"
        Open.ShowDialog()
        Try
            AppManager.CurrentBrowser.Navigate(Open.FileName)
        Catch ex As Exception
            'Do nothing on Exception
        End Try
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowPrintDialog()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowPrintPreviewDialog()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        AppManager.MainForm = Me
        If Not WebBrowser1.TabPages.Count = 0 Then
            Try
                AppManager.CurrentBrowser.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text & "&btnG=Google+Search&meta=")
            Catch ex As Exception
                Dim cForm As Form = WebBrowser1.SelectedForm
                If TypeOf cForm Is frmBrowser Then
                    Dim ofrm As frmBrowser = cForm
                    AppManager.CurrentBrowser = ofrm.WebBrowser1
                    If WebBrowser1.TabPages.Count = 1 Then
                        If WebBrowser1.TabPages(0).CloseButtonVisible = True Then
                            WebBrowser1.TabPages(0).CloseButtonVisible = False
                        End If
                    End If
                    AppManager.CurrentBrowser.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text & "&btnG=Google+Search&meta=")
                    Me.Text = cForm.Text & " - BBrowser (Tabs Enabled)"
                    txtUrl.Text = ofrm.WebBrowser1.Url.ToString
                    tsbBack.Enabled = AppManager.CurrentBrowser.CanGoBack
                    tsbForward.Enabled = AppManager.CurrentBrowser.CanGoForward
                Else
                    MsgBox("An Error has Occurred!", MsgBoxStyle.Critical, "Error!")
                End If
            End Try
        ElseIf WebBrowser1.TabPages.Count = 0 Then
            Dim ofrm As New frmBrowser
            Dim oTab As New BBrowser.TabPage(ofrm)
            AppManager.AddTab(oTab)

            ofrm.WebBrowser1.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text & "&btnG=Google+Search&meta=")
        End If
    End Sub

    Private Sub PropertiesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PropertiesToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowPropertiesDialog()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AppManager.MainForm = Me
        AboutBox1.ShowDialog()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        AppManager.MainForm = Me
        My.Computer.Clipboard.Clear()
        My.Computer.Clipboard.SetText(txtUrl.Text)
    End Sub

    Private Sub SaveWebpageAsHTMLFileToYourComputerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveWebpageAsHTMLFileToYourComputerToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowSaveAsDialog()
    End Sub

    Private Sub SearchBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SearchBox.KeyDown
        AppManager.MainForm = Me
        If (e.KeyCode = Keys.Enter) Then
            If Not WebBrowser1.TabPages.Count = 0 Then
                Try
                    AppManager.CurrentBrowser.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text)
                Catch ex As Exception
                    Dim cForm As Form = WebBrowser1.SelectedForm
                    If TypeOf cForm Is frmBrowser Then
                        Dim ofrm As frmBrowser = cForm
                        AppManager.CurrentBrowser = ofrm.WebBrowser1
                        If WebBrowser1.TabPages.Count = 1 Then
                            If WebBrowser1.TabPages(0).CloseButtonVisible = True Then
                                WebBrowser1.TabPages(0).CloseButtonVisible = False
                            End If
                        End If
                        AppManager.CurrentBrowser.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text)
                        Me.Text = cForm.Text & " - BBrowser (Tabs Enabled)"
                        txtUrl.Text = ofrm.WebBrowser1.Url.ToString
                        tsbBack.Enabled = AppManager.CurrentBrowser.CanGoBack
                        tsbForward.Enabled = AppManager.CurrentBrowser.CanGoForward
                    Else
                        MsgBox("An Error has Occurred!", MsgBoxStyle.Critical, "Error!")
                    End If
                End Try
            ElseIf WebBrowser1.TabPages.Count = 0 Then
                Dim ofrm As New frmBrowser
                Dim oTab As New BBrowser.TabPage(ofrm)
                AppManager.AddTab(oTab)

                ofrm.WebBrowser1.Navigate("http://www.google.co.uk/search?hl=en&q=" & SearchBox.Text)
            End If
        End If
    End Sub

    Private Sub ClickHereToViewYourHistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClickHereToViewYourHistoryToolStripMenuItem.Click
        AppManager.MainForm = Me
        HistoryForTabs.ShowDialog()
    End Sub

    Private Sub Browser_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        e.Cancel = True
        If Not WebBrowser1.TabPages.Count = 1 And 0 Then
            Dim closeallthetabs As New CloseAllTabs
            CloseAllTabs.ShowDialog()

            If CloseAllTabs.DialogResult = Windows.Forms.DialogResult.Yes Then
                e.Cancel = False
                My.Settings.IsFormLoading = True
                My.Settings.IsFirstTimeLoad = True
                My.Settings.Save()
                Try
                    If My.Settings.WindowsOpen = 1 Then
                        My.Settings.IsFormOpenTabs = False
                        My.Settings.WindowsOpen = 0
                        If Not IsNothing(AppManager.CurrentBrowser) Then
                            AppManager.CurrentBrowser.Dispose()
                        End If
                        My.Settings.Save()
                        Application.Exit()
                        Exit Sub
                    ElseIf My.Settings.WindowsOpen = 0 Then
                        Exit Sub
                    Else
                        My.Settings.WindowsOpen -= 1
                    End If
                Catch ex As Exception

                End Try
            ElseIf CloseAllTabs.DialogResult = Windows.Forms.DialogResult.Cancel Then
                'Continue surfing on BBrowser
            End If

            'Select Case closeallthetabs.DialogResult
            'Case DialogResult.Yes
            'e.Cancel = False
            'End
            'Case DialogResult.No
            'WebBrowser1.TabPages.RemoveAt(WebBrowser1.SelectedIndex)
            'WebBrowser1.SelectTab(WebBrowser1.TabPages.Count - 1)
            'i = i - 1
            'Case DialogResult.Cancel
            'Continue surfing on BBrowser
            'End Select
        Else
            e.Cancel = False
            My.Settings.IsFormLoading = True
            My.Settings.IsFirstTimeLoad = True
            My.Settings.Save()
            Try
                If My.Settings.WindowsOpen = 1 Then
                    My.Settings.IsFormOpenTabs = False
                    My.Settings.WindowsOpen = 0
                    If Not IsNothing(AppManager.CurrentBrowser) Then
                        AppManager.CurrentBrowser.Dispose()
                    End If
                    'BBrowser.frmBrowser.Close()
                    My.Settings.Save()
                    Application.Exit()
                    Exit Sub
                ElseIf My.Settings.WindowsOpen = 0 Then
                    Exit Sub
                Else
                    My.Settings.WindowsOpen -= 1
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    'Private Sub Loading(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserProgressChangedEventArgs)
    'With webProgress
    '.Visible = True
    ' End With
    'webProgress.Maximum = e.MaximumProgress
    'webProgress.Value = e.CurrentProgress
    'webStatus.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).StatusText
    'txtUrl.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri
    'Me.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle & " - BBrowser (Tabs Enabled)"
    'WebBrowser1.SelectedTab.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
    'Dim CurProg As Single
    'Dim MaxProg As Single
    'CurProg = e.CurrentProgress
    'MaxProg = e.MaximumProgress
    'webProgress.Value = CInt((CurProg / MaxProg) * 100)
    'End Sub

    'Private Sub browse_done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs)
    'WebBrowser1.SelectedTab.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
    'txtUrl.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).Url.AbsoluteUri
    'Me.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle & " - BBrowser (Tabs Enabled)"
    'If CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString = "about:blank" Then
    'WebBrowser1.SelectedTab.Text = "Blank Page"
    'End If
    'With webProgress
    '.Value = 0
    '.Visible = False
    'End With
    'webStatus.Text = CType(WebBrowser1.SelectedTab.Controls.Item(0), WebBrowser).StatusText
    'CurrentUrl()
    'My.Settings.BrowserHistory.Add(My.Settings.CurrentUrlForTabs)
    'End Sub

    Private Sub NewTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewTabToolStripMenuItem.Click
        AppManager.MainForm = Me
        Try
            If WebBrowser1.TabPages(0).CloseButtonVisible = False Then
                WebBrowser1.TabPages(0).CloseButtonVisible = True
            End If
        Catch ex As Exception

        End Try
        Dim ofrm As New frmBrowser
        Dim oTab As New BBrowser.TabPage(ofrm)
        AppManager.AddTab(oTab)

        If My.Settings.ShowHomePageAtStartUp = "Yes" Then
            If My.Settings.UseBBrowserHomePage = "True" Then
                ofrm.WebBrowser1.Navigate(My.Settings.BBrowserHomePageUrl)
            ElseIf My.Settings.UseBBrowserHomePage = "False" Then
                ofrm.WebBrowser1.GoHome()
            End If
        ElseIf My.Settings.ShowHomePageAtStartUp = "No (Show Blank Page)" Then
            ofrm.WebBrowser1.Navigate("about:blank")
        ElseIf My.Settings.ShowHomePageAtStartUp = "No (Show Google)" Then
            ofrm.WebBrowser1.Navigate("http://www.google.co.uk")
        End If
    End Sub

    Public Sub HistoryForTabsNavigate()
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.Navigate(HistoryForTabs.ListBox1.SelectedItem.ToString)
    End Sub

    Public Sub CurrentUrl()
        AppManager.MainForm = Me
        AppManager.CurrentURLSettings()
    End Sub

    Public Sub CurrentTitle()
        AppManager.MainForm = Me
        AppManager.CurrentTitleSettings()
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.Refresh()
    End Sub

    Private Sub StopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.Stop()
    End Sub

    Private Sub HomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeToolStripMenuItem.Click
        AppManager.MainForm = Me
        If My.Settings.UseBBrowserHomePage = "True" Then
            AppManager.CurrentBrowser.Navigate(My.Settings.BBrowserHomePageUrl)
        ElseIf My.Settings.UseBBrowserHomePage = "False" Then
            AppManager.CurrentBrowser.GoHome()
        End If
    End Sub

    Private Sub BBrowserSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BBrowserSettingsToolStripMenuItem.Click
        AppManager.MainForm = Me
        BBrowserSettings.ShowDialog()
    End Sub

    Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowPageSetupDialog()
    End Sub

    Private Sub BBrowserNewsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BBrowserNewsToolStripMenuItem.Click
        AppManager.MainForm = Me
        BBrowserNews.Show()
    End Sub

    'Public Sub NewWindow3(ByRef pDisp As Object, ByRef cancel As Boolean, ByVal flags As Integer, _
    'ByVal URLContext As String, ByVal URL As String)

    'Try
    'If WebBrowser1.TabPages(0).CloseButtonVisible = False Then
    'WebBrowser1.TabPages(0).CloseButtonVisible = True
    'End If
    'Catch ex As Exception

    'End Try
    'Dim ofrm As New frmBrowser
    'Dim oTab As New BBrowser.TabPage(ofrm)
    'AppManager.AddTab(oTab)
    'AppManager.CurrentBrowser.Navigate(URL)
    'cancel = True
    'End Sub

    'Private Sub Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs)
    'With webProgress
    '.Visible = False
    '.Value = 0
    'End With
    'End Sub

    Public Sub LoadFavourites()
        Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
        tvFavs.BeginUpdate()
        tvFavs.Nodes.Clear()
        'Load favourites from all sub-directories
        LoadFolders(New System.IO.DirectoryInfo(Path), Nothing)
        'Load the favourites from the favourites folder
        LoadPath(Path, Nothing)
        'Use ContextMenuStrip1 as Favourites Context Menu Strip
        tvFavs.ContextMenuStrip = ContextMenuStrip1
        'Start FileSystemWatcher
        FileSystemWatcher1.Path = Path
        FileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.DirectoryName Or NotifyFilters.FileName
        FileSystemWatcher1.EnableRaisingEvents = True
        'Repaint
        tvFavs.EndUpdate()
    End Sub

    Public Sub LoadFolders(ByVal dirInfo As System.IO.DirectoryInfo, _
    ByVal currentNode As TreeNode)

        AppManager.MainForm = Me
        Dim objNode As System.Windows.Forms.TreeNode

        Dim objDir As System.IO.DirectoryInfo

        For Each objDir In dirInfo.GetDirectories()
            If currentNode Is Nothing Then
                objNode = tvFavs.Nodes.Add(objDir.Name, objDir.Name, 0, 1)
                objNode.Tag = String.Empty
            Else
                objNode = currentNode.Nodes.Add(objDir.Name, objDir.Name, 0, 1)
                objNode.Tag = String.Empty
            End If
            'Set the full path of the folder
            objNode.Tag = objDir.FullName

            If objDir.GetDirectories().Length = 0 Then
                'This Node has no further sub directories
                LoadPath(objDir.FullName, objNode)
            Else
                'Add this folder to the current node and continue processing sub-directories
                LoadFolders(objDir, objNode)
                LoadPath(objDir.FullName, objNode)
            End If
        Next objDir
    End Sub

    Public Sub LoadPath(ByVal strPath As String, _
    ByVal currentNode As TreeNode)

        AppManager.MainForm = Me
        Dim oNode As TreeNode
        Dim name As String
        Dim objDir As New System.IO.DirectoryInfo(strPath)
        Dim SmallIco As IntPtr
        Dim shinfo As SHFILEINFO
        shinfo = New SHFILEINFO
        'Process each file in the path with a .url extension
        Dim objFile As System.IO.FileInfo
        For Each objFile In objDir.GetFiles("*.url")
            oNode = New TreeNode
            'get the icon
            shinfo.szDislayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
            'Get the small icon
            SmallIco = SHGetFileInfo(objFile.FullName, 0, shinfo, _
                        Marshal.SizeOf(shinfo), _
                        SHGFI_ICON Or SHGFI_SMALLICON)
            'Set the text property to the friendly name
            name = Path.GetFileNameWithoutExtension(objFile.Name)
            Dim oIcon As Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

            Try
                'attempt 1
                tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
            Catch nullex As NullReferenceException
                Exit Try
            Catch ex As Exception
                Try
                    'attempt2
                    tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
                Catch ex1 As Exception
                    Try
                        'attempt3
                        tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
                    Catch ex2 As Exception
                        Try
                            'all attempts failed try all again
                        Catch ex3 As Exception
                            LoadPathAttempt2(strPath, currentNode)
                            Exit Sub
                        End Try
                    End Try
                End Try
            End Try
            oNode.Text = name
            oNode.Tag = objFile.FullName
            oNode.ImageKey = name
            oNode.SelectedImageKey = name
            If currentNode Is Nothing Then
                tvFavs.Nodes.Add(oNode)
            Else
                currentNode.Nodes.Add(oNode)
            End If
            DestroyIcon(oIcon.Handle)
        Next objFile
    End Sub

    Public Sub LoadPathAttempt2(ByVal strPath As String, _
    ByVal currentNode As TreeNode)

        AppManager.MainForm = Me
        Dim oNode As TreeNode
        Dim name As String
        Dim objDir As New System.IO.DirectoryInfo(strPath)
        Dim SmallIco As IntPtr
        Dim shinfo As SHFILEINFO
        shinfo = New SHFILEINFO
        'Process each file in the path with a .url extension
        Dim objFile As System.IO.FileInfo
        For Each objFile In objDir.GetFiles("*.url")
            oNode = New TreeNode
            'get the icon
            shinfo.szDislayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
            'Get the small icon
            SmallIco = SHGetFileInfo(objFile.FullName, 0, shinfo, _
                        Marshal.SizeOf(shinfo), _
                        SHGFI_ICON Or SHGFI_SMALLICON)
            'Set the text property to the friendly name
            name = Path.GetFileNameWithoutExtension(objFile.Name)
            Dim oIcon As Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)

            Try
                'attempt 1
                tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
            Catch ex As Exception
                Try
                    'attempt2
                    tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
                Catch ex1 As Exception
                    Try
                        'attempt3
                        tvFavs.ImageList.Images.Add(name, oIcon.ToBitmap)
                    Catch ex2 As Exception
                        Throw New Exception("An error occurred while adding an image to tvFavs.ImageList")
                    End Try
                End Try
            End Try
            oNode.Text = name
            oNode.Tag = objFile.FullName
            oNode.ImageKey = name
            oNode.SelectedImageKey = name
            If currentNode Is Nothing Then
                tvFavs.Nodes.Add(oNode)
            Else
                currentNode.Nodes.Add(oNode)
            End If
            DestroyIcon(oIcon.Handle)
        Next objFile
    End Sub

    Private Sub OpenOrganiseFavouritesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenOrganiseFavouritesToolStripMenuItem.Click
        AppManager.CurrentBrowser.ShowOrganizeFavorites()
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Try
            Me.Invoke(New EventHandler(AddressOf fsw_Reload))
        Catch ex As Exception
            Try
                Me.Invoke(New EventHandler(AddressOf fsw_Reload))
            Catch ex1 As Exception
                Try
                    Me.Invoke(New EventHandler(AddressOf fsw_Reload))
                Catch ex2 As Exception
                    FileSystemWatcher1.EnableRaisingEvents = False
                    LoadFavourites()
                End Try
            End Try
        End Try
    End Sub

    Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        Try
            Me.Invoke(New EventHandler(AddressOf fsw_Reload))
        Catch ex As Exception
            Try
                Me.Invoke(New EventHandler(AddressOf fsw_Reload))
            Catch ex1 As Exception
                Try
                    Me.Invoke(New EventHandler(AddressOf fsw_Reload))
                Catch ex2 As Exception
                    FileSystemWatcher1.EnableRaisingEvents = False
                    LoadFavourites()
                End Try
            End Try
        End Try
    End Sub

    Private Sub FileSystemWatcher1_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted
        Try
            Me.Invoke(New EventHandler(AddressOf fsw_Reload))
        Catch ex As Exception
            Try
                Me.Invoke(New EventHandler(AddressOf fsw_Reload))
            Catch ex1 As Exception
                Try
                    Me.Invoke(New EventHandler(AddressOf fsw_Reload))
                Catch ex2 As Exception
                    FileSystemWatcher1.EnableRaisingEvents = False
                    LoadFavourites()
                End Try
            End Try
        End Try
    End Sub

    Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
        Try
            Me.Invoke(New EventHandler(AddressOf fsw_Reload))
        Catch ex As Exception
            Try
                Me.Invoke(New EventHandler(AddressOf fsw_Reload))
            Catch ex1 As Exception
                Try
                    Me.Invoke(New EventHandler(AddressOf fsw_Reload))
                Catch ex2 As Exception
                    FileSystemWatcher1.EnableRaisingEvents = False
                    LoadFavourites()
                End Try
            End Try
        End Try
    End Sub

    Private Sub fsw_Reload(ByVal source As Object, ByVal args As EventArgs)
        FileSystemWatcher1.EnableRaisingEvents = False
        LoadFavourites()
    End Sub

    Private Sub tvFavs_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvFavs.MouseMove

        AppManager.MainForm = Me

        ' Get a reference to the TreeView control

        Dim objTreeView As TreeView = DirectCast(sender, TreeView)

        ' Get a reference to the node under the mouse 

        ' pointer (if there is one)

        Dim objNode As TreeNode = objTreeView.GetNodeAt(e.X, e.Y)

        If objNode IsNot Nothing Then

            Dim tagtext As String = objNode.Tag

            If tagtext.EndsWith(".url") Then
                ' We're over a URL

                objTreeView.Cursor = Cursors.Hand
            Else
                ' We're over a folder

                objTreeView.Cursor = Cursors.[Default]
            End If
        Else
            ' We're over an empty region in the TreeView

            objTreeView.Cursor = Cursors.[Default]
        End If
    End Sub

    Private Sub tvFavs_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvFavs.NodeMouseClick

        AppManager.MainForm = Me

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Exit Sub
        End If
        If e.Node.Nodes.Count > 0 Then
            e.Node.Expand()
        Else
            If e.Node.ImageIndex = 0 Then
                Exit Sub
            Else
                Dim obj As New tlxIni(e.Node.Tag)
                AppManager.CurrentBrowser.Navigate(obj.GetString("INTERNETSHORTCUT", "URL", String.Empty))
            End If
        End If
    End Sub

    Private Sub CheckForUpdatesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckForUpdatesToolStripMenuItem.Click
        AppManager.MainForm = Me
        If MsgBox("Would you like to check for updates? If you are using a nightly then the updater only checks if there is a new 'stable' release. This updater does not check for new nightly builds.", MsgBoxStyle.YesNo, "BBrowser Updater") = MsgBoxResult.No Then
            Exit Sub
        End If
        Try
            My.Computer.Network.DownloadFile("http://bbrowser.weebly.com/uploads/4/3/7/1/4371454/latestversionbbrowser.txt", "C:\latestversionbbrowser.txt")
        Catch ex As Exception

        End Try

        If My.Computer.FileSystem.FileExists("C:\latestversionbbrowser.txt") Then
            'Continue
        Else
            MsgBox("Could not connect to the internet to check for updates. Please check your internet connection and try again.", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If
        Dim isupdateavailable As String = My.Computer.FileSystem.ReadAllText("C:\latestversionbbrowser.txt")

        Label2.Visible = False
        Label2.Text = isupdateavailable
        If Not Label2.Text = "Ultimate 1.0" Then
            MsgBox("An update is available. Please close BBrowser and run BBrowser Installer to update your browser.", MsgBoxStyle.OkOnly, "Update Available")
        Else
            MsgBox("No updates are available.", MsgBoxStyle.Information, "BBrowser Updater")
            Exit Sub
        End If

        My.Computer.FileSystem.DeleteFile("C:\latestversionbbrowser.txt")
    End Sub

    Private Sub AddToFavouritesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AppManager.MainForm = Me
        AddToFavTabs.ShowDialog()
    End Sub

    Private Sub WebBrowser1_SelectedTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser1.SelectedTabChanged
        On Error Resume Next
        AppManager.MainForm = Me
        Dim cForm As Form = WebBrowser1.SelectedForm
        If TypeOf cForm Is frmBrowser Then
            Dim ofrm As frmBrowser = cForm
            Me.Text = cForm.Text & " - BBrowser (Tabs Enabled)"
            txtUrl.Text = ofrm.WebBrowser1.Url.ToString
            AppManager.CurrentBrowser = ofrm.WebBrowser1
            tsbBack.Enabled = AppManager.CurrentBrowser.CanGoBack
            tsbForward.Enabled = AppManager.CurrentBrowser.CanGoForward
            If WebBrowser1.TabPages.Count = 1 Then
                If WebBrowser1.TabPages(0).CloseButtonVisible = True Then
                    WebBrowser1.TabPages(0).CloseButtonVisible = False
                End If
            End If
        Else

        End If
    End Sub

    Private Sub AddToFavouritesToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToFavouritesToolStripMenuItem.Click
        AppManager.MainForm = Me
        AddToFavTabs.ShowDialog()
    End Sub

    Private Sub OrganiseFavouritesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrganiseFavouritesToolStripMenuItem.Click
        AppManager.MainForm = Me
        AppManager.CurrentBrowser.ShowOrganizeFavorites()
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        AppManager.MainForm = Me
        If My.Settings.TimesLoadUntilNewsReminder <= 0 Then
            Timer3.Stop()
            MsgBox("Remember to check the news section! It can contain important (and sometimes critical) information about BBrowser. It is recommended to check BBrowser News every few days.", MsgBoxStyle.OkOnly, "Reminder")
            My.Settings.TimesLoadUntilNewsReminder = 4
            Timer3.Enabled = False
        Else
            Timer3.Stop()
            Timer3.Enabled = False
        End If
    End Sub

    Public Sub OpenNewWindow()
        Dim frm As New BrowserWithoutTabs
        frm.Show()
    End Sub

    Private Sub BrowserWithoutTabs_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        AppManager.MainForm = Me
    End Sub

    Private Sub BrowserWithoutTabs_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        AppManager.MainForm = Me
    End Sub

    Private Sub ReportABugToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReportABugToolStripMenuItem.Click
        AppManager.MainForm = Me
        Try
            If WebBrowser1.TabPages(0).CloseButtonVisible = False Then
                WebBrowser1.TabPages(0).CloseButtonVisible = True
            End If
        Catch ex As Exception

        End Try
        Dim ofrm As New frmBrowser
        Dim oTab As New BBrowser.TabPage(ofrm)
        AppManager.AddTab(oTab)

        ofrm.WebBrowser1.Navigate("http://code.google.com/p/simplicity-browser/issues/entry")
    End Sub
End Class